package com.jlu.springsecurity.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService implements UserDetailsService {

  @Autowired
  private PasswordEncoder pw;

  @Override
  public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

    System.out.println("name" + name);

    if (!"will".equals(name)){
      throw new UsernameNotFoundException("用户名错误");
    }
    String password = pw.encode("123");
    System.out.println("pw");
    return new User(name,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc,/main.html,/insert,/delete"));
  }
}
